// 插入文章的自定义菜单
import { IDomEditor, IButtonMenu } from '@wangeditor/editor';
import { ARTICLE_SVG } from '../../icon-svg';
import { isLinkDisabled } from '../../isDisabledMenusConfig';
import { IExtendConfig } from '../interface';

function getInsertLinkConfig(editor: IDomEditor) {
  const { EXTEND_CONF } = editor.getConfig();
  const { insertLinkConfig } = EXTEND_CONF as IExtendConfig;
  return insertLinkConfig;
}

class InsertArticle implements IButtonMenu {
  tag: string;
  title: string;
  iconSvg: string;
  constructor() {
    this.title = '文章';
    this.iconSvg = ARTICLE_SVG;
    this.tag = 'button';
  }

  // 菜单是否需要激活（如选中加粗文本，“加粗”菜单会激活），用不到则返回 false
  isActive(editor: IDomEditor): boolean {
    // TS 语法
    return false;
  }

  // 获取菜单执行时的 value ，用不到则返回空 字符串或 false
  getValue(editor: IDomEditor): string | boolean {
    // TS 语法
    return '';
  }

  // 菜单是否需要禁用（如选中 H1 ，“引用”菜单被禁用），用不到则返回 false
  isDisabled(editor: IDomEditor): boolean {
    // TS 语法
    // return false;
    return isLinkDisabled(editor);
  }

  // 点击菜单时触发的函数
  exec(editor: IDomEditor, value: string | boolean) {
    if (this.isDisabled(editor)) return;
    console.log('点击了自定义菜单-文章链接');
    const { showInsertArticleModal } = getInsertLinkConfig(editor);
    showInsertArticleModal(editor,'article');

    // editor.emit('insertCustomLink', {
    //   type: 'article',
    // });
  }
}

export default InsertArticle;
